<?php
namespace app\index\model;
use think\Db;
/**
 * Created by Submint Text.
 * User: hetaoren<452649510@qq.com>
 * Date: 2017/03/10
 * Explain: 广告业务处理
 */
class ads extends Base{
	/**
	 * 分页
	 */
	public function pageQuery(){
		$page = $this->order('ad_position asc, ad_id desc,ad_sort asc')->paginate(input('pagesize/d'))->toArray();
		if(count($page['Rows'])>0){
			foreach ($page['Rows'] as $key => $v){
				$page['Rows'][$key]['start_date'] = date('Y-m-d',$v['start_date']);
				$page['Rows'][$key]['end_date'] = date('Y-m-d',$v['end_date']);
			}
		}
		return $page;
	}
	public function getById($id){
		return $this->get($id);
	}
	/**
	 * 新增
	 */
	public function add(){
		$data = input('post.');
		$data['start_date'] = strtotime($data['start_date']);
		$data['end_date'] = strtotime($data['end_date']);
		$data['create_time'] = time();
		HTRUnset($data,'ad_id');
		$result = $this->allowField(true)->save($data);
    	if(false !== $result){
    	    return HTRReturn("新增成功", 1);
    	}else {
            return HTRReturn('新增失败'); 
        }        
	} 
    /**
	 * 编辑
	 */
	public function edit(){
		$data = input('post.');
		$data['start_date'] = strtotime($data['start_date']);
		$data['end_date'] = strtotime($data['end_date']);
		HTRUnset($data,'create_time');
	    $result = $this->allowField(true)->save($data,['ad_id'=>(int)$data['ad_id']]);
        if(false !== $result){
        	return HTRReturn("编辑成功", 1);
        }else{
        	return HTRReturn('编辑失败'); 
        }
	}
	/**
	 * 删除
	 */
    public function del(){
	    $id = (int)input('post.id/d');
	    $result = $this->destroy($id);
        if(false !== $result){
        	return HTRReturn("删除成功", 1);
		}else{
            return HTRReturn('删除失败');
        }
	}
	/**
	* 修改广告排序
	*/
	public function changeSort(){
		$id = (int)input('id');
		$ad_sort = (int)input('ad_sort');
		$result = $this->setField(['ad_id'=>$id,'ad_sort'=>$ad_sort]);
		if(false !== $result){
        	return HTRReturn("操作成功", 1);
        }else{
        	return HTRReturn($this->getError());
        }
	}
	
}
